package top.xiaoshuaila.moments.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.moments.mapper.MessageMapper;
import top.xiaoshuaila.moments.pojo.RespBean;

import java.util.Objects;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public RespBean getmes(long id,int page) {
        return RespBean.success("success",messageMapper.getMessages(id,page*5,5));
    }

    @Override
    public RespBean clearmes(long id) {
        if(redisTemplate.opsForValue().get("id-" + id)!=null)
        redisTemplate.delete(Objects.requireNonNull(redisTemplate.opsForValue().get("id-" + id)));
        redisTemplate.delete("id-"+id);
        return RespBean.success("success",messageMapper.clearMessages(id));
    }

    @Override
    public int gettotal(long id) {
        return messageMapper.gettotal(id);
    }
}
